package com.cd.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.News;
import com.cd.bean.User;
import com.cd.service.news.NewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
/**
 * 新闻动态
 *@Author zhk
 *@Date 2017-12-19
 **/
@RequestMapping("/news")
@Controller
public class NewsController {

	
	@Autowired
	NewsService newsService;
	
	/**
	 *跳转到添加新闻页面
	 * @return
	 */
	@RequestMapping("/goAdd")
	public ModelAndView saveView(){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("admin/xwdt/news_add");
		return mv;
	}
	
	/**
	 * 新增
	 * @param ntitle
	 * @param ncontent
	 * @param nlevel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/save")
	public ModelAndView save(@RequestParam(value="NEWS_TITLE")String title,
            @RequestParam(value="NEWS_CONTENT")String content,
            @RequestParam(value="NEWS_LEVEL")Integer level,@RequestParam(value="NEWS_RESOURCE")String resource) throws Exception {
		ModelAndView mv = new ModelAndView();
		//int total = newsService.size();
		News news = new News();
		//news.setNEWS_ID(total+1);
		news.setNEWS_CONTENT(content);
	    news.setNEWS_RESOURCE(resource);
		news.setNEWS_TITLE(title);
		news.setNEWS_LEVEL(level);
		news.setPUBLISH_DATE(new Date());
		//news.setNEWS_IMG(nEWS_IMG);
		newsService.save(news);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/delete")
	public ModelAndView delete(@RequestParam(value="id")Integer id) throws Exception{
		News news=newsService.findById(id);
		ModelAndView mv = new ModelAndView();
		newsService.delete(id);
		mv.setViewName("redirect:list.do");
		return mv;
	}
	
	/**
	 * 查询（带分页）
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listNews")
	public ModelAndView findAllNews(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 15);//开始分页
	    List<News> list = newsService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("info/xwdt/xwdt");
		return mv;
	}
	
	/**
	 * 通过id查询
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/findById")
	public ModelAndView findById(@RequestParam(value = "id") Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		News news = null;
		News nextNews = null;
		News preNews = null;
		int total = newsService.size();//获取总记录
		int nextId = id+1;
		int preId = id-1;
		if(id <= total){
			if(id >= 1){
				news = newsService.findById(id);//当前条
				preNews = newsService.findById(preId);//前一条
				nextNews = newsService.findById(nextId);//下一条	
			}else {
				news = newsService.findById(id+1);
			}
		}else{
			news = newsService.findById(id-1);
		}
		
		mv.addObject("news", news);
		mv.addObject("preNews", preNews);
		mv.addObject("nextNews", nextNews);
		mv.setViewName("info/xwdt/xwdt_show");
		return mv;
	}
	
	
	
	
	/**
	 * 跳转新闻动态
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goNews")
	public ModelAndView goNews() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("info/xwdt/xwdt");
		return mv;
	}
	
	
	
	/**
	 *后台显示新闻列表 
	 * @param pn
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public ModelAndView list(@RequestParam(value = "pn",defaultValue="1")Integer pn) throws Exception {
		ModelAndView mv = new ModelAndView();
		PageHelper.startPage(pn, 10);//开始分页,每页10条记录
	    List<News> list = newsService.listAll();
		PageInfo page = new PageInfo(list,5);//设置分页信息
		mv.addObject("pageInfo", page);
		mv.setViewName("admin/xwdt/news_list");
		return mv;
	}
	
	
	/**
	 * 修改
	 * @param id
	 * @param username
	 * @param content
	 * @param email
	 * @param phone
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/edit")
	public ModelAndView eidt(@RequestParam(value="NEWS_ID")Integer id,@RequestParam(value="NEWS_TITLE")String title,
			                 @RequestParam(value="NEWS_CONTENT")String content
			                ) throws Exception {
			ModelAndView mv = new ModelAndView();
			News news = newsService.findById(id);
			news.setNEWS_TITLE(title);
			news.setNEWS_CONTENT(content);
			news.setPUBLISH_DATE(new Date());
			newsService.edit(news);
			mv.setViewName("redirect:list.do");
		return mv;
	}
	
	/**
	 * 去查看新闻内容页面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goContent")
	public ModelAndView goContent(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		News news = newsService.findById(id);
		//System.out.println(news.getNEWS_ID());
		mv.addObject("news", news);
		mv.setViewName("admin/xwdt/news_content");
		return mv;
	}
	
	/**
	 * 去编辑界面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/goEdit")
	public ModelAndView goEdit(@RequestParam(value="id")Integer id) throws Exception {
		ModelAndView mv = new ModelAndView();
		News news = newsService.findById(id);
		mv.addObject("news", news);
		mv.setViewName("admin/xwdt/news_edit");
		return mv;
	}
	
	 /** 
     * 批量删除 
     */  
   /* @SuppressWarnings("null")  
    @RequestMapping(value="/deleteBatch")  
    public @ResponseBody Integer  deleteBatch(@RequestParam(value="array") Integer[] array){    
         if (array == null && array.length <= 0) {  
                return 0;  
            }  
        return newsService.deleteBatch(array);  
    }  */
    /**
     * 
     * 批量删除
     * @author hxy
     * @param request
     * @param response
     */
	@RequestMapping("/del")  
	public void batchDeletes(HttpServletRequest request, HttpServletResponse response) {  
	    String items = request.getParameter("delitems");
	    //System.out.println(items);  
	    String[] strs = items.split(",");  
	  
	    for (int i = 0; i < strs.length; i++) {  
	        try {  
	            int a = Integer.parseInt(strs[i]);  
	            newsService.delete(a);  
	        } catch (Exception e) {  
	        }  
	    }  
	}
}
